package com.qingxin.park.action;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.entity.Provenance;
import com.qingxin.park.entity.ProvenanceImgs;
import com.qingxin.park.service.FarmParkService;
import com.qingxin.park.service.ProvenanceImgsService;
import com.qingxin.park.service.ProvenanceService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.MyUploadFile;
import com.qingxin.sys.entity.MyUploadFileParam;
import com.qingxin.sys.entity.MyUploadFiles;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;

/**
 * 地块
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/provenance")
public class ProvenanceAction extends BaseAction {
	
	@Autowired
	private FarmParkService farmParkService;
	@Autowired
	private ProvenanceService provenanceService;
	@Autowired
	private ProvenanceImgsService provenanceImgsService;
	
	/**
	 * 列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "trace/provenance/provenance_list";
	}
	
	/*@RequestMapping(value="/lefttree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public List<TreeNode> lefttree(String pid, HttpServletRequest request){
		List<TreeNode> list = new ArrayList<TreeNode>();
		if(StringUtils.isEmpty(pid)){
			TreeNode root = new TreeNode("0", "所有园区","PARK", null, true);
			list.add(root);
		}else{
			list.addAll(provenanceService.getUITreeData(getLoginUser(request)));
		}
		return list;
	}
	*/
	/**
	 * 获取列表分页数据
	 * @param parkid
	 * @param blockName
	 * @param isuse
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(@RequestParam(value="leftdata")String parkid,String datatype,
			 String proName, String subName, PageParam pageParam, HttpServletRequest request){
		return provenanceService.list(parkid,datatype, proName, subName, pageParam, getLoginUser(request));
	}
	
	
	/**
	 * 添加产地环境信息
	 * @param model
	 * @param parkid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, @RequestParam(value="leftdata")String parkid, HttpServletRequest request){
		//model.put("proid", proid);
		FarmPark farmPark = farmParkService.get(parkid);
		model.put("farmPark", farmPark);
		
		return "trace/provenance/provenance_add";
	}
	
	
	/**
	 * 保存地块信息
	 * @param block
	 * @param result
	 * @param parkid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid  Provenance provenance,BindingResult result, String proid, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		SysError info = null;
		MyUploadFileParam uploadFileParam = new MyUploadFileParam("provenanceimg", request);
		
		info = provenanceService.add(provenance,uploadFileParam, getLoginUser(request), request);
		
		return info;
	}

	
	/**
	 * 修改地块信息
	 * @param model
	 * @param blockid
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String proid){
		Provenance provenance = provenanceService.get(proid);
		model.put("provenance", provenance);
		
		List<ProvenanceImgs> list = provenanceImgsService.listprovenanceImgs(proid);
		MyUploadFiles provenanceImgs = new MyUploadFiles();
		for(ProvenanceImgs img : list){
			provenanceImgs.addFile(new MyUploadFile(img.getId(), img.getImgPath(), img.getImgName(), img.getImgDesc(), img.getThumb()));
		}
		model.put("provenanceImgs", provenanceImgs);
		
		return "trace/provenance/provenance_edit";
	}
	
	
	/**
	 * 保存修改信息
	 * @param block
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid Provenance provenance, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		MyUploadFileParam uploadFileParam = new MyUploadFileParam("provenanceimg", request);
		SysError info = provenanceService.update(provenance,uploadFileParam, getLoginUser(request), request);
		return info;
	}
	
	/**
	 * 删除产地环境信息
	 * @param blockid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] proid, HttpServletRequest request){
		if(proid == null || proid.length == 0){
			return new SysError("没有选择任何信息.");
		}
		MyUploadFileParam uploadFileParam = new MyUploadFileParam("provenanceimg", request);
		return provenanceService.delete(proid,uploadFileParam, getLoginUser(request), request);
	}
	
	
	/**
	 * 判断产地环境名称是否存在
	 * @param blockName
	 * @param blockid
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value="/isNameDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isNameDuplicate(String proName, String proid, String parkid){
		boolean existed = provenanceService.isNameDuplicate(proName, proid, parkid);
		return existed ? "1":"0";
	}
	
	
	/**
	 * 查看产地环境
	 * @param model
	 * @param proid
	 * @return
	 */
	@RequestMapping(value="/show.do", method=RequestMethod.GET)
	public String show(ModelMap model, String proid){
		Provenance provenance = provenanceService.get(proid);
		model.put("provenance", provenance);
		
		List<ProvenanceImgs> list = provenanceImgsService.listprovenanceImgs(proid);
		model.put("provenanceImgs", list);

		return "trace/provenance/provenance_show";
	}
	
	
}
